package org.check.Util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.check.VO.DBConnVO;
import org.check.VO.DBParaVO;
import org.check.loadProperties.LoadProperty;

public class LoadUtil {
	
	public static String fileBaseName = ".sql";
	public static String userRegex = "@USER_NAME";
	
	public static void main(String[] args) {
		DBParaVO vo = new DBParaVO();
		vo.setStartDate("20160101");
		vo.setEndDate("20161220");
		vo.setUsername("bblearn");
		vo.setTablename("courseuser");
		vo.setType("oracle");
		vo.setVersion("SP8");
		//System.out.println("@@");
		/*String sqlStr = getSql(vo);
		System.out.println(sqlStr);*/
		Map<String, String> nameArrtMap = getNameAttrMap();
		for (Object o : nameArrtMap.entrySet()) {
            Map.Entry entry = (Map.Entry) o;
            System.out.println(entry.getKey().toString());
            System.out.println(entry.getValue().toString());
        }
	}
	
	public static List<String> getFileNameList(DBParaVO vo){
		List<String> list = new ArrayList<String>();
		String filename = "/config/optimize/"+vo.getType()+"/"+vo.getVersion();
		String fileBaseName = System.getProperty("user.dir");
		File file = new File(fileBaseName+filename);
        String [] names = file.list();
        for(String fn:names){
        	if(!fn.split("\\.")[0].equals("courseuser"))
        		list.add(fn.split("\\.")[0]);
        }	
		return list;
	}
	
	public static LinkedHashMap<String,String> getNameAttrMap(){
		OrderedProperties prop = LoadProperty.loadProp();     
        LinkedHashMap<String,String> map1 = new LinkedHashMap<String,String>();
        LinkedHashMap<String,String> map2 = new LinkedHashMap<String,String>();
        Iterator<Entry<Object, Object>> it = prop.entrySet().iterator();  
        String regex = ".*[\u6570]$|.*[\u91cf]$";
        
        while(it.hasNext()){
        	Entry<Object, Object> entry = it.next(); 
        	String key = entry.getKey().toString();
            if(key.matches(regex))
            	map2.put(entry.getKey().toString(), entry.getValue().toString());
            else 
            	map1.put(entry.getKey().toString(), entry.getValue().toString());
        }
        map1.putAll(map2);
		return map1;
	}
	
	public static String getSql(DBParaVO vo){
		String sqlStr = "";
		String filename = "/config/optimize/"+vo.getType()+"/"+vo.getVersion()+"/"+vo.getTablename()+fileBaseName;
		//System.out.println(filename);
        StringBuilder sb = new StringBuilder(); 
        
		InputStreamReader in = null;
		try {
			File f = new File(System.getProperty("user.dir")+filename);
			FileInputStream  ins = new FileInputStream (f);
			in = new InputStreamReader(ins,"UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
        try {   
        	BufferedReader reader = new BufferedReader(in);         
            String line = null;     
            while ((line = reader.readLine()) != null) {   
                sb.append(line + "\n");     
            }  
            sqlStr = sb.toString();
        } catch (IOException e) {   
            e.printStackTrace();   
        }  
        
        sqlStr = sqlStr.replaceAll(userRegex, vo.getUsername());
        return sqlStr;
	}
	
	public static List<DBConnVO> getDbConnVoList(){
		ArrayList<DBConnVO> list = new ArrayList<DBConnVO>();
		
		
		return list;
	}
}
